package controller

import (
	"danmu/common"
	"danmu/model"
	"danmu/response"

	"github.com/gin-gonic/gin"
)

func GetImage(ctx *gin.Context) {
	name := ctx.DefaultQuery("name", "0")
	if name == "0" {
		response.Fail(ctx, nil, "图片不存在")
		return
	}
	fileName := "./file/img/" + name
	ctx.File(fileName)
}

func GetCover(ctx *gin.Context) {
	name := ctx.DefaultQuery("name", "0")
	if name == "0" {
		response.Fail(ctx, nil, "图片不存在")
		return
	}
	fileName := "./file/cover/" + name
	ctx.File(fileName)
}

func GetUserFace(ctx *gin.Context) {
	name := ctx.DefaultQuery("name", "0")
	if name == "0" {
		response.Fail(ctx, nil, "图片不存在")
		return
	} else if name == "default" {
		fileName := "./file/face/" + "default.jpg"
		ctx.File(fileName)
		return
	}
	fileName := "./file/face/" + name
	ctx.File(fileName)
}

func GetVideo(ctx *gin.Context) {
	DB := common.GetDB()
	var video model.Video
	vid := ctx.DefaultQuery("vid", "0")
	if vid == "0" {
		response.Fail(ctx, nil, "视频不存在")
		return
	}
	//根据id查找视频名称
	DB.Where("id = ?",vid).First(&video)
	if video.ID == 0{
		response.Fail(ctx, nil, "视频不存在")
		return
	}
	fileName := "./file/video/" + video.VideoName
	ctx.File(fileName)
}